<?php namespace Home\Controller; 
use Think\Controller;

/**
 * Class 会员设置控制器
 * @package Home\Controller
 */
class SetController extends CommonController
{

    public function index()
    {   
        $id=$_SESSION['id'];
        $sql="select * from user where id='{$id}'";
        $res=M()->query($sql);
        $res=current($res);
        $this->assign('info',$res);
        $this->display();

    }
    /**
     * [EditInfo 信息修改]
     */
    public function EditInfo()
    {   
        $id=$_SESSION['id'];
        //检测邮箱账号
        $email=$_POST['email'];
        $sql="select email from user where id<>'{$id}' and email='{$email}'";
        $res=M()->query($sql);
        if ($res) {
            $arr = array(
                'error'=>1,
                'info'=>'邮箱己存在,请重新输入！'
                );
            echo json_encode($arr);
            exit;
        }
        //检测昵称
        $nickname=$_POST['nickname'];
        $sql="select nickname from user where id<>'{$id}' and nickname='{$nickname}'";
        $res=M()->query($sql);
        if ($res) {
            
            $arr = array(
                'error'=>1,
                'info'=>'昵称己存在，请重新输入！'
                );
            echo json_encode($arr);
            exit;
        }



        //获取POST信息
        $data=$_POST;
        //账号全部转小写
        $data['email'] =strtolower($data['email']);
        //邮箱激活状态
        $data['estate']=0;
        //更新数据信息
        if (M('user')->where("id='$id'")->save($data)) {
            $_SESSION['nickname']=$_POST['nickname'];
            $arr = array(
                'error'=>0,
                'info'=>'修改成功！'
                );
            echo json_encode($arr);
            exit;
        }else{
            $arr = array(
                'error'=>0,
                'info'=>'修改成功！'
                );
            echo json_encode($arr);
            exit;
        }

    }
    /**
     * [EditPassword 修改密码]
     */
    public function EditPassword()
    {
        //POST 获取两次密码
        $password=$_POST['password'];
        $repassword=$_POST['repassword'];

        //判断两次密码是否相同
        if ($password!=$repassword) {
            $arr = array(
                'error'=>1,
                'info'=>'两次密码不一致'
                );
            echo json_encode($arr);
            exit;
        }

        //POST获取表单密码
        $data['password']=md5($_POST['password']);
        $id=$_SESSION['id'];
        //更新密码
        if (M('user')->where("id='$id'")->save($data)) {
            $arr = array(
                'error'=>0,
                'info'=>'修改成功！'
                );
            echo json_encode($arr);
            exit;
        }else{
            $arr = array(
                'error'=>1,
                'info'=>'修改失败,新密码与原密码相同！'
                );
            echo json_encode($arr);
            exit;
        }

    }

    /**
     * [Upload 修改头像]
     */
    public function upload(){

        $id=$_SESSION['id'];
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Uploads/face/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件 
        $info   =   $upload->upload();
        $info= current($info);
        $path =__ROOT__.'/Uploads/face/'.$info['savepath'].$info['savename'];
        $res=M('user')->where("id='$id'")->save(array ('face'=>$path));
        if($res) { //上传成功
            $_SESSION['face']=$path;
            echo '头像上传成功';
        }else{// 上传失败                       
            echo 0;
        }

    }

    public function sendemail() {


        $id=$_SESSION['id'];
        $sql="select email,nickname from user where id='{$id}'";
        $res=M('user')->query($sql);
        $res=current($res);        
        $email=$res['email'];
        $nickname=$res['nickname'];
        $title='九维社区邮箱激活';
        $url="http://bbs.9wi.net".U('home/mail/Verifyemail',array('verify'=>md5($email)));

        $content=<<<Eof
        <table border="0"
        cellpadding="0" cellspacing="0" style="width: 600px; border: 1px solid
        #ddd; border-radius: 3px; color: #555; font-family: 'Helvetica Neue
        Regular',Helvetica,Arial,Tahoma,'Microsoft YaHei','San
        Francisco','微软雅黑','Hiragino Sans GB',STHeitiSC-Light; font-size: 12px;
        height: auto; margin: auto; overflow: hidden; text-align: left; word-
        break: break-all; word-wrap: break-word;"> <tbody style="margin: 0;
        padding: 0;"> <tr style="background-color: #393D49; height: 60px; margin:
        0; padding: 0;"> <td style="margin: 0; padding: 0;"> <div style="color:
        #5EB576; margin: 0; margin-left: 30px; padding: 0;"><a style="font-size:
        14px; margin: 0; padding: 0; color: #5EB576; text-decoration: none;"
        href="http://bbs.9wi.net／" target="_blank">九维 -
        问答社区</a></div> </td> </tr> <tr style="margin: 0; padding: 0;">
        <td style="margin: 0; padding: 30px;"> <p style="line-height: 20px;
        margin: 0; margin-bottom: 10px; padding: 0;"> Hi，<em style="font-weight:
        700;">{$nickname}</em>，请完成以下操作激活邮箱： </p> <div style=""> <a href="{$url}" style="background-color: #009E94; color: #fff; display: inline-
        block; height: 32px; line-height: 32px; margin: 0 15px 0 0; padding: 0
        15px; text-decoration: none;" target="_blank">立即验证 激活邮箱</a> </div> <p style
        ="line-height: 20px; margin-top: 20px; padding: 10px; background-color:
        #f2f2f2; font-size: 12px;"> 如果该邮件不是由你本人操作，请勿进行激活！否则你的邮箱将会被他人绑定。 </p> </td>
        </tr> <tr style="background-color: #fafafa; color: #999; height: 35px;
        margin: 0; padding: 0; text-align: center;"> <td style="margin: 0;
        padding: 0;">系统邮件，请勿直接回复。</td> </tr> </tbody> </table>
Eof;

        if (!empty($email)){
            $res = bbsemail($email,$title,$content);
            if ($res==0) {
                $arr=array(
                'error'=>1,
                'info'=>'已成功将激活链接发送到了您的邮箱，接受可能会稍有延迟，请注意查收!'
                );
            echo json_encode($arr);
            exit(); 
             }else{
                $arr=array(
                'error'=>2,
                'info'=>'发送失败，请稍候再试！'
                );
            echo json_encode($arr);
            exit();
             }

        }else{
            $arr=array(
                'error'=>0,
                'info'=>'邮箱未设置，请先设置邮箱!'
                );
            echo json_encode($arr);
            exit();
        }
         
    }
        


}